package com.xh.ssm.controller;

import com.xh.ssm.bean.Role;
import com.xh.ssm.service.RoleService;
import com.xh.ssm.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/1/26/026
 */
@RequestMapping("/role")
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public String list() {
        return "role/role-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Role role) {
        return roleService.getPage(page, limit, role);
    }

    @GetMapping("/edit")
    public String edit(Integer id, Model model) {
        if (id != null) {
            model.addAttribute("role", roleService.getById(id));
        }
        return "role/role-update";
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(Role role) {
        roleService.saveOrUpdate(role);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        roleService.deleteById(id);
        return R.ok();
    }

    @GetMapping("/select")
    @ResponseBody
    public R select() {
        return roleService.getList();
    }
}
